package cn.jbolt.admin.demotable;

import cn.jbolt.core.cache.JBoltRoleCache;
import cn.jbolt.core.poi.excel.JBoltExcel;
import cn.jbolt.core.poi.excel.JBoltExcelHeader;
import cn.jbolt.core.poi.excel.JBoltExcelSheet;
import cn.jbolt.core.service.base.JBoltBaseService;
import cn.jbolt.core.api.httpmethod.JBoltHttpGet;
import cn.jbolt.core.api.httpmethod.JBoltHttpPost;
import cn.jbolt.core.api.HttpMethod;
import cn.jbolt.core.api.httpmethod.JBoltHttpMethod;
import cn.jbolt.core.model.base.JBoltBaseModel;
import com.jfinal.aop.Inject;
import cn.jbolt.core.api.JBoltApiBaseController;
import cn.jbolt.core.permission.CheckPermission;
import cn.jbolt.extend.permission.PermissionKey;
import cn.jbolt.core.permission.UnCheckIfSystemAdmin;
import com.jfinal.core.Path;
import com.jfinal.aop.Before;
import com.jfinal.core.paragetter.Para;
import com.jfinal.plugin.activerecord.tx.Tx;
import cn.jbolt.admin.demotable.model.DemoTable;
import cn.jbolt.core.permission.UnCheck;
import com.jfinal.upload.UploadFile;

import java.util.Date;
import java.util.List;

/**
 * demo测试表
 * @ClassName: DemoTableAdminController
 * @author: 山东小木
 * @date: 2023-10-27 15:45
 */
//@CheckPermission(PermissionKey.DEMO_TABLE)
@UnCheckIfSystemAdmin
@Path("/api/admin/demoTable")
public class DemoTableAdminController extends JBoltApiBaseController {

	@Inject
	private DemoTableService service;
	@Override
    protected JBoltBaseService<? extends JBoltBaseModel<?>> getModelService() {
        return service;
    }
   /**
    * 单个详情
    */
   @JBoltHttpGet
   public void index() {
       renderJBoltApiRet(findModelById());
   }

   /**
   	 * 数据查询接口
   	 * @param page       第几页
   	 * @param pageSize   每页几条数据
   	 * @param keywords   关键词
   	 * @param sortColumn 排序列名
   	 * @param sortType   排序方式 asc desc
     * @param sex 性别
     * @param enable 是否启用
     * @param startTime 开始时间
     * @param type 类型
     * @param state 状态
     * @param createTime 创建时间
   	 */
	@JBoltHttpMethod({HttpMethod.GET,HttpMethod.POST})
	public void datas(@Para(value = PAGE_PARAM_NAME,defaultValue = "1")Integer page,
    @Para(value = PAGESIZE_PARAM_NAME,defaultValue = "15")Integer pageSize,
        @Para(KEYWORDS_PARAM_NAME)String keywords,
        @Para(value = "sortColumn",defaultValue = "id")String sortColumn,
        @Para(value = "sortType",defaultValue = "desc")String sortType,
        @Para("sex")Integer sex,
        @Para("enable")Boolean enable,
        @Para("startTime")Date startTime,
        @Para("type")Integer type,
        @Para("state")Integer state,
        @Para("createTime")Date createTime) {
		renderJBoltApiSuccessWithData(service.getAdminDatas(page,pageSize,keywords,sortColumn,sortType,sex,enable,startTime,type,state,createTime));
	}


	public void export(@Para(KEYWORDS_PARAM_NAME)String keywords,
					   @Para(value = "sortColumn",defaultValue = "id")String sortColumn,
					   @Para(value = "sortType",defaultValue = "desc")String sortType,
					   @Para("sex")Integer sex,
					   @Para("enable")Boolean enable,
					   @Para("startTime")Date startTime,
					   @Para("type")Integer type,
					   @Para("state")Integer state,
					   @Para("createTime")Date createTime) {
		List<DemoTable> list = service.getAllAdminDatas(keywords, sortColumn, sortType, sex, enable, startTime, type, state, createTime);
		renderBytesToExcelXlsFile(JBoltExcel.create().setSheets(
				JBoltExcelSheet.create("DemoTable").setHeaders(1, JBoltExcelHeader.create("name", "姓名", 20),
								JBoltExcelHeader.create("user_name","用户名" ,20),
								JBoltExcelHeader.create("phone","手机号" ,30),
								JBoltExcelHeader.create("email","E-mail",30 ),
								JBoltExcelHeader.create("age","年龄" ,10),
								JBoltExcelHeader.create("roleNames","角色",30 ),
								JBoltExcelHeader.create("hobbys","爱好",30 ),
								JBoltExcelHeader.create("brief_info","简介",50 )
				).setDataChangeHandler((data, i) -> {
					data.change("roleNames", JBoltRoleCache.me.getNames(data.getStr("roles")));
						})
						.setModelDatas(2, list)
		).toByteArray(), "list.xls");
	}


   /**
	* 保存
	*/
	@Before(Tx.class)
	@JBoltHttpPost
	public void save(@Para("")DemoTable demoTable) {
		renderJBoltApiRet(service.save(demoTable));
	}

   /**
	* 更新
	*/
	@Before(Tx.class)
    @JBoltHttpPost
	public void update(@Para("")DemoTable demoTable) {
		renderJBoltApiRet(service.update(demoTable));
	}

	/**
	 * 更新
	 */
	@Before(Tx.class)
	@JBoltHttpPost
	public void updateColumn(@Para("") DemoTable demoTable) {
		renderJBoltApiRet(service.updateOneColumn(demoTable));
	}

   /**
	* 删除
	*/
	@Before(Tx.class)
    @JBoltHttpGet
	public void delete() {
	    renderJBoltApiRet(deleteModelById(true));
	}

   /**
	* 切换启用状态
	*/
	@Before(Tx.class)
	public void toggleEnable() {
		renderJBoltApiRet(toggleModelEnableById());
	}

   	/**
   	 * autocomplete组件专用数据
   	 * @param keywords  查询关键字
   	 * @param limit     返回前limit条匹配数据
   	 * @param always    如果没有输入keywords 是否默认出limit条数据
   	 */
   	@UnCheck
   	public void autocompleteDatas(@Para("q")String keywords,
   								  @Para(value = "limit",defaultValue = "20")Integer limit,
   								  @Para(value = "always",defaultValue = "true")Boolean always) {
		renderJBoltApiSuccessWithData(service.getAutocompleteList(keywords, limit,always,"name,sn,title"));
   	}

   /**
	* 得到select radio checkbox专用options数据
	*/
	@UnCheck
	public void options() {
       renderJBoltApiSuccessWithData(service.getOptionListEnable("name","id"));
	}


	/**
	 * 下载导入模版
	 */
	public void downloadTpl(){
		renderBytesToExcelXlsxFile(JBoltExcel.create().setFileName("Demo演示导入模版").setXlsx(true).setSheets(
				JBoltExcelSheet.create("Demo").setHeaders(1,
						JBoltExcelHeader.create(DemoTable.SN,"编号",20),
						JBoltExcelHeader.create(DemoTable.NAME,"姓名",20),
						JBoltExcelHeader.create(DemoTable.USER_NAME,"用户名",20),
						JBoltExcelHeader.create(DemoTable.PHONE,"手机号",30),
						JBoltExcelHeader.create(DemoTable.EMAIL,"邮箱",20),
						JBoltExcelHeader.create(DemoTable.SEX,"性别",10),
						JBoltExcelHeader.create(DemoTable.AGE,"年龄",10),
						JBoltExcelHeader.create(DemoTable.SCORE,"积分",10)
				)

		));
	}

	/**
	 * 执行导入
	 */
	@Before(Tx.class)
	public void importExcel(){
		UploadFile uploadFile = getFile();
		if(uploadFile == null || notExcel(uploadFile)){
			renderJBoltApiFail("请上传excel文件");
			return;
		}

		renderJBoltApiRet(service.importExcel(uploadFile.getFile()));
	}

}
